package org.oschina.api.blog.catalog.dao;

import java.util.List;
import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.blog.catalog.model.BlogCatalog;
import org.oschina.api.blog.catalog.vo.BlogCatalogVo;
import org.oschina.api.user.user.dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BlogCatalogDAO extends BaseDAO {
	
	@Autowired
	private UserDAO userDAO;

	private final String findVo[]={"id","user_id","name","count","create_time"};
	
	/**
	 * 根据用户返回
	 * @param user
	 * @return
	 */
	public List<BlogCatalogVo> findVoByUser(int user)
	{
		if(isNeedCache(user))
		{
			return getQueryHelper().query_cache(getByUserCache(user),Constants.TIMEOUT_BLOG_CATALOG,
					findVo, new String[]{"user_id=?"}, null, new Object[]{user}, BlogCatalogVo.class, -1, -1);
		}
		
		return getQueryHelper().query(findVo, new String[]{"user_id=?"}, null, new Object[]{user}, BlogCatalogVo.class, -1, -1);
	}
	private String getByUserCache(int user)
	{
		return getKey(Constants.CACHE_BLOG_CATALOG_BY_AUTHOR, user);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public BlogCatalogVo findVoById(int id)
	{
		return (BlogCatalogVo) getQueryHelper().queryForObject(findVo, new String[]{"id=?"}, new Object[]{id}, BlogCatalogVo.class);
	}
	private boolean isNeedCache(int userId)
	{
		return (userDAO.isInCache(userId) || userDAO.isOnline(userId));
	}
	
	/**
	 * 更新数量
	 * @param id
	 * @param count
	 */
	public void updateCount(int id,int count)
	{
		getCacheHelper().hincr(getIdKey(id), "count", count);
		batchUpdate_queue("UPDATE "+Constants.DB_BLOG_CATALOG+" SET count=count+? WHERE id=?", new Object[]{count,id});
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int delete(int id)
	{
		getCacheHelper().del(getIdKey(id));
		return (int) getQueryHelper().delete(new String[]{"id=?"}, new Object[]{id},BlogCatalogVo.class);
	}
	
	/**
	 * 创建
	 * @param catalog
	 * @return
	 */
	public BlogCatalog create(BlogCatalog catalog)
	{
		if(isNeedCache(catalog.getUser_id()))
		{
			getCacheHelper().hset(getIdKey(catalog.getId()), catalog, BlogCatalog.class,Constants.TIMEOUT_BLOG_CATALOG);
			getCacheHelper().rpush(getByUserCache(catalog.getUser_id()), catalog.getId(), Constants.TIMEOUT_BLOG_CATALOG);
		}
		
		getQueryHelper().insert(new String[]{"id","user_id","name","count","create_time"}, 
				new Object[]{catalog.getId(),catalog.getUser_id(),catalog.getName(),catalog.getCount(),catalog.getCreate_time()},BlogCatalogVo.class);
		return catalog;
	}
	
	/**
	 * 更新
	 * @param catalog
	 * @return
	 */
	public int update(BlogCatalog catalog)
	{
		if(isNeedCache(catalog.getUser_id()))
			getCacheHelper().hset(getIdKey(catalog.getId()), "name", catalog.getName(), Constants.TIMEOUT_BLOG_CATALOG);
		
		return (int) getQueryHelper().update(new String[]{"name=?"}, 
				new String[]{"id=?"}, 
				new Object[]{catalog.getName(),catalog.getId()},BlogCatalogVo.class);
	}
	
	private String getIdKey(Object id)
	{
		return getKey(Constants.CACHE_BLOG_CATALOG,id);
	}
}